package com.company.biz.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.biz.user.mapper.DepartmentDao;
import com.company.biz.user.service.DepartmentService;
import com.company.common.enums.ExecuteErrorEnum;
import com.company.common.enums.TableNamePrefixEnum;
import com.company.common.exception.BizException;
import com.company.common.util.IdentifierGeneratorUtil;
import com.company.user.entity.DepartmentEntity;
import com.company.user.req.AddDepartmentReq;
import com.company.user.req.DepartmentInfoReq;
import com.company.user.req.DepartmentListReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentDao, DepartmentEntity> implements DepartmentService {

    /**
     * 获取所有的公司
     *
     * @return
     */
    @Override
    public List<DepartmentEntity> getDepartmentList(DepartmentListReq req) {
        LambdaQueryWrapper<DepartmentEntity> eq = new QueryWrapper<DepartmentEntity>()
                .lambda().eq(DepartmentEntity::getIsDel, "n")
                .eq(DepartmentEntity::getDepartmentFid, req.getDepartmentId());
        return list(eq);
    }

    @Override
    public boolean addDepartment(AddDepartmentReq req) {
        DepartmentEntity departmentEntity = new DepartmentEntity();
        req.copyTo(departmentEntity);
        Date date = new Date();
        departmentEntity.setDepartmentId(IdentifierGeneratorUtil.getOwnId(TableNamePrefixEnum.TBDEPARTMENT));
        departmentEntity.setCreateTime(date);
        departmentEntity.setUpdateTime(date);
        departmentEntity.setDepartmentStatus(0);
        return save(departmentEntity);
    }

    @Override
    public DepartmentEntity getDepartmentInfoByDepartmentId(String departmentId) {
        return this.getById(departmentId);
    }

    @Override
    public boolean updateDepartmentInfoByDepartmentId(DepartmentInfoReq req) {
        DepartmentEntity departmentEntity = new DepartmentEntity();
        req.copyTo(departmentEntity);
        departmentEntity.setUpdateTime(new Date());
        return saveOrUpdate(departmentEntity, new QueryWrapper<DepartmentEntity>().lambda().eq(DepartmentEntity::getDepartmentId, req.getDepartmentId()));
    }

    @Override
    public boolean deleteDepartmentByDepartmentId(String departmentId) {
        boolean update = update(new UpdateWrapper<DepartmentEntity>().lambda()
                .eq(DepartmentEntity::getDepartmentId, departmentId)
                .set(DepartmentEntity::getIsDel, "y"));
        return update;
    }

    @Override
    public List<Tree<String>> getAllDepartmentTree() {
        //条件构造器
        QueryWrapper<DepartmentEntity> likeQW = new QueryWrapper<>();
        likeQW.lambda().eq(DepartmentEntity::getIsDel, 0);
        //获取数据
        List<DepartmentEntity> likeList = list(likeQW);
        if (CollUtil.isEmpty(likeList)) {
            return null;
        }
        //树形结构操作
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        for (DepartmentEntity DepartmentEntity : likeList) {
            nodeList.add(new TreeNode<>(DepartmentEntity.getDepartmentId(), DepartmentEntity.getDepartmentFid(), DepartmentEntity.getDepartmentName(), DepartmentEntity.getSort()));
        }
        //构建树形
        TreeNodeConfig config = new TreeNodeConfig();
        config.setIdKey("departmentId");
        config.setParentIdKey("departmentFid");
        config.setNameKey("departmentName");
        config.setWeightKey("sort");
        return TreeUtil.build(nodeList, "none", config,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                });
    }
}
